import axios from 'axios'
let height = 775.9

export const requireImages = async (name) => {

  let o = {}
  console.log('o: ', o)
  await new Promise(async (res, rej) => {
    const req = await import.meta.glob('/public/2024_07_11_data/**/img2/*.{png,jpg,jpeg}')
    console.log('req: ', req);
    const reqTxt = await import.meta.glob('/public/2024_07_11_data/**/photo_info/*.{txt,}')
    console.log('reqTxt: ', reqTxt);
    const imagesArray = Object.keys(req)
    const txtArray = Object.keys(reqTxt)

    for (let index = 0; index < txtArray.length; index++) {
      let path = txtArray[index]
      let groupKey = getGroupKey(path)
      let imgNum = getImgNum(path)
      if (!o[groupKey]) o[groupKey] = {}

      await axios.get(path).then(async (res) => {
        let d = res.data.split(' ').map((a) => {
          // if (a.length > 11) (+a).toFixed(6)
          return  +(+a).toFixed(6)
        })
        console.log(' d : ', d)
        let obj = {
          scale: d[0],
          start: [+d[1], d[3] - height, -d[2]],
          end: [+d[4], d[6] - height, -d[5]],
          rightP: [d[7], d[8], d[9]].map((i) => Number(i)),
          leftP: [d[10], d[11], d[12]].map((i) => Number(i)),
          lookAtP: [d[13], d[14], d[15]].map((i) => Number(i)),
        }
        o[groupKey][imgNum] = obj
      })
    }




    imagesArray.forEach((path) => {
      const groupKey = getGroupKey(path)
      const imgNum = getImgNum(path)
      let ex = getImg(path)
      if (!o[groupKey]) o[groupKey] = {}
      if (!o[groupKey][imgNum]) o[groupKey][imgNum] = {}
      o[groupKey][imgNum]['src'] = path.replace('/public', '')
      o[groupKey][imgNum]['name'] = ex
    })
    res(0)
  })
  return o

}


// 函数来提取分组依据
function getGroupKey(path) {
  // 正则表达式匹配 `/public/2024_07_11_data` 后面的部分作为分组依据
  const regex = /^\/public\/2024_07_11_data\/([^/]+)/
  const match = path.match(regex)
  if (match && match[1]) {
    return match[1]
  } else {
    return null // 如果路径格式不符合预期，返回 null 或者可以抛出错误
  }
}

// 函数来提取分组依据
function getImgNum(path) {
  // 正则表达式匹配 `/public/2024_07_11_data` 后面的部分作为分组依据
  const regex = /\/(\d+)\./
  const match = path.match(regex)
  if (match && match[1]) {
    return match[1]
  } else {
    return null // 如果路径格式不符合预期，返回 null 或者可以抛出错误
  }
}

// 函数来提取分组依据
function getImg(path) {
  // 正则表达式匹配 `/public/2024_07_11_data` 后面的部分作为分组依据
  const regex = /\/([^/]+)$/
  const match = path.match(regex)
  if (match && match[1]) {
    return match[1]
  } else {
    return null // 如果路径格式不符合预期，返回 null 或者可以抛出错误
  }
}